<template>
  <div class="header">
    <div class="back-btn" @click="goBack">← 返回</div>
    <h1>创建监考报名批次</h1>
    <div class="breadcrumb">监考管理 / 创建批次</div>
  </div>
  <div class="create-batch">
    <!-- 顶部标题栏 -->
    <div class="header">
      <h1>创建监考报名批次</h1>
      <div class="breadcrumb">监考管理 / 创建批次</div>
    </div>

    <!-- 表单区域 -->
    <div class="form-container">
      <div class="form-section">
        <label>批次名称<span class="required">*</span>：</label>
        <input
            type="text"
            v-model="batchData.name"
            placeholder="请输入批次名称"
            class="form-input"
        >
      </div>

      <div class="form-section">
        <label>关联年份<span class="required">*</span>：</label>
        <select v-model="batchData.year" class="form-select">
          <option value="" disabled selected>请选择关联年份</option>
          <option v-for="year in years" :key="year" :value="year">{{ year }}年</option>
        </select>
      </div>

      <div class="form-section">
        <label>批次开始时间<span class="required">*</span>：</label>
        <div class="datetime-picker">
          <input
              type="date"
              v-model="batchData.startDate"
              class="date-input"
          >
          <input
              type="time"
              v-model="batchData.startTime"
              class="time-input"
          >
        </div>
      </div>

      <div class="form-section">
        <label>批次结束时间<span class="required">*</span>：</label>
        <div class="datetime-picker">
          <input
              type="date"
              v-model="batchData.endDate"
              class="date-input"
          >
          <input
              type="time"
              v-model="batchData.endTime"
              class="time-input"
          >
        </div>
      </div>

      <div class="form-section">
        <label>监考说明：</label>
        <textarea
            v-model="batchData.description"
            placeholder="请输入监考说明..."
            class="form-textarea"
        ></textarea>
      </div>

      <div class="form-section">
        <label>上传附件：</label>
        <div class="upload-area">
          <div class="upload-box" @click="triggerFileInput">
            <div class="upload-icon">+</div>
            <div class="upload-text">上传文件</div>
          </div>
          <div class="upload-hint">
            限制上传文件10个以下，单个大小不超过100M
          </div>
          <input
              type="file"
              ref="fileInput"
              @change="handleFileUpload"
              multiple
              hidden
          >

          <!-- 文件列表 -->
          <div v-if="attachments.length > 0" class="file-list">
            <div v-for="(file, index) in attachments" :key="index" class="file-item">
              <div class="file-icon">📄</div>
              <div class="file-info">
                <div class="file-name">{{ file.name }}</div>
                <div class="file-size">{{ formatFileSize(file.size) }}</div>
              </div>
              <button class="remove-btn" @click="removeFile(index)">×</button>
            </div>
          </div>
        </div>
      </div>

      <div class="form-actions">
        <button class="submit-btn" @click="submitForm">提交</button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();

// 添加返回方法
const goBack = () => {
  router.go(-1);
};

// 批次数据
const batchData = ref({
  name: '',
  year: '',
  startDate: '',
  startTime: '',
  endDate: '',
  endTime: '',
  description: ''
});

// 年份选项
const years = ref([
  '2023', '2024', '2025', '2026', '2027'
]);

// 附件管理
const attachments = ref<File[]>([]);
const fileInput = ref<HTMLInputElement | null>(null);

// 触发文件选择
const triggerFileInput = () => {
  if (fileInput.value) {
    fileInput.value.click();
  }
};

// 处理文件上传
const handleFileUpload = (event: Event) => {
  const target = event.target as HTMLInputElement;
  if (target.files && target.files.length > 0) {
    // 检查文件数量限制
    if (attachments.value.length + target.files.length > 10) {
      alert('最多只能上传10个文件');
      return;
    }

    // 检查文件大小
    for (let i = 0; i < target.files.length; i++) {
      const file = target.files[i];
      if (file.size > 100 * 1024 * 1024) {
        alert(`文件 "${file.name}" 超过100M限制`);
        continue;
      }
      attachments.value.push(file);
    }

    // 重置文件输入
    target.value = '';
  }
};

// 移除文件
const removeFile = (index: number) => {
  attachments.value.splice(index, 1);
};

// 格式化文件大小
const formatFileSize = (bytes: number) => {
  if (bytes === 0) return '0 Bytes';
  const k = 1024;
  const sizes = ['Bytes', 'KB', 'MB', 'GB'];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
};

// 提交表单
const submitForm = () => {
  // 验证必填字段
  if (!batchData.value.name) {
    alert('请填写批次名称');
    return;
  }

  if (!batchData.value.year) {
    alert('请选择关联年份');
    return;
  }

  if (!batchData.value.startDate || !batchData.value.startTime) {
    alert('请填写批次开始时间');
    return;
  }

  if (!batchData.value.endDate || !batchData.value.endTime) {
    alert('请填写批次结束时间');
    return;
  }

  // 验证时间逻辑
  const startDateTime = new Date(`${batchData.value.startDate}T${batchData.value.startTime}`);
  const endDateTime = new Date(`${batchData.value.endDate}T${batchData.value.endTime}`);

  if (startDateTime >= endDateTime) {
    alert('批次结束时间必须晚于开始时间');
    return;
  }

  // 提交数据（这里只是模拟）
  console.log('提交批次数据:', batchData.value);
  console.log('附件:', attachments.value);

  // 模拟提交成功
  alert('批次创建成功！');
  router.push('/dashboard3/exam-management');
};
</script>

<style scoped>
.create-batch {
  background-color: white;
  padding: 25px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.header {
  margin-bottom: 30px;
  padding-bottom: 15px;
  border-bottom: 2px solid #1a3a6c;
}

.header h1 {
  margin: 0;
  font-size: 24px;
  color: #1a2b4b;
}

.breadcrumb {
  font-size: 14px;
  color: #666;
  margin-top: 8px;
}

.form-container {
  max-width: 800px;
  margin: 0 auto;
}

.form-section {
  margin-bottom: 25px;
}

.form-section label {
  display: block;
  font-weight: 600;
  color: #1a2b4b;
  margin-bottom: 10px;
}

.required {
  color: #f5222d;
  margin-left: 4px;
}

.form-input, .form-select, .form-textarea {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 15px;
  transition: border-color 0.3s;
}

.form-input:focus, .form-select:focus, .form-textarea:focus {
  border-color: #1a3a6c;
  outline: none;
}

.form-textarea {
  min-height: 120px;
  resize: vertical;
}

.datetime-picker {
  display: flex;
  gap: 15px;
}

.date-input, .time-input {
  flex: 1;
  padding: 12px 15px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 15px;
}

.upload-area {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  padding: 20px;
  background-color: #fafafa;
}

.upload-box {
  width: 120px;
  height: 120px;
  border: 2px dashed #8c8c8c;
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s;
  margin-bottom: 15px;
}

.upload-box:hover {
  border-color: #1a3a6c;
  background-color: #f0f7ff;
}

.upload-icon {
  font-size: 36px;
  color: #8c8c8c;
  line-height: 1;
}

.upload-text {
  color: #8c8c8c;
  margin-top: 10px;
  font-size: 14px;
}

.upload-hint {
  color: #8c8c8c;
  font-size: 14px;
  margin-bottom: 15px;
}

.file-list {
  margin-top: 20px;
}

.file-item {
  display: flex;
  align-items: center;
  padding: 10px 15px;
  background-color: #f8fafc;
  border-radius: 4px;
  border: 1px solid #e8e8e8;
  margin-bottom: 10px;
}

.file-icon {
  font-size: 24px;
  margin-right: 12px;
}

.file-info {
  flex: 1;
}

.file-name {
  font-weight: 500;
  margin-bottom: 4px;
}

.file-size {
  color: #666;
  font-size: 13px;
}

.remove-btn {
  background: none;
  border: none;
  font-size: 24px;
  color: #999;
  cursor: pointer;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.3s;
}

.remove-btn:hover {
  color: #f5222d;
}

.form-actions {
  margin-top: 40px;
  text-align: center;
}

.submit-btn {
  padding: 12px 50px;
  background-color: #1a3a6c;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.3s;
}

.submit-btn:hover {
  background-color: #0d2a56;
}

/* 添加返回按钮样式 */
.back-btn {
  position: absolute;
  top: 25px;
  left: 25px;
  font-size: 20px;
  cursor: pointer;
  color: #1a3a6c;
}
</style>